package com.im.config;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.util.Calendar;
import java.util.HashMap;

@Configuration
public class JwtToken {

    @Value("${jwt.key}")
    private String JWT_KEY;

    public String getToken(HashMap<String, String> map) {
        // JWT
        // 设置过期时间
        Calendar instance = Calendar.getInstance();
//        instance.add(Calendar.HOUR, 24 * 7);
        // 秒
        instance.add(Calendar.SECOND, 60 * 60 * 24 * 7);

        // 生成令牌
        JWTCreator.Builder builder = JWT.create();
        for (String m : map.keySet()) {
            builder.withClaim(m, map.get(m));//设置携带内容
        }
        builder.withExpiresAt(instance.getTime());//设置过期时间
        String token = builder.sign(Algorithm.HMAC256(JWT_KEY));//设置签名私钥

        return token;
    }
}
